//
//  SceneChangeViewController.swift
//  ZSceneDemo
//
//  Created by 张鑫 on 2020/9/8.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

import UIKit

import SceneKit
import SpriteKit

class SceneChangeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeSceneFile))

        loadModelFile()
    }
    
    var sceneView: SCNView?
    func loadModelFile() {
        
        //设置摄像机架设点
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(0, 0, 5)
        
        //设置光源点
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(0, 10, 10)
        
        //设置周围环境
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light?.type = .ambient
        ambientLightNode.light?.color = UIColor.lightGray
        
        // 创建场景，添加各个元素
        let scene = SCNScene(named: "art.scnassets/untitled.dae")
        scene?.rootNode.addChildNode(cameraNode)
        scene?.rootNode.addChildNode(lightNode)
        scene?.rootNode.addChildNode(ambientLightNode)
        
        sceneView = SCNView(frame: CGRect.zero)
        sceneView?.translatesAutoresizingMaskIntoConstraints = false
        sceneView?.scene = scene
        sceneView?.allowsCameraControl = true
        //        threeDView?.showsStatistics = true
        sceneView?.backgroundColor = UIColor.cyan
        view.addSubview(sceneView!)
        
        
        sceneView?.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        sceneView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        sceneView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        sceneView?.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
    }
    
    var lastScene: SCNScene?
    @objc func changeSceneFile() {
        lastScene = sceneView?.scene
        
        guard let scene = SCNScene(named: "art.scnassets/ship.scn") else {
            return
        }
        
        //设置摄像机架设点
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(0, 0, 15)
        
        //设置光源点
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(0, 10, 10)
        
        //设置周围环境
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light?.type = .ambient
        ambientLightNode.light?.color = UIColor.lightGray
        
        scene.rootNode.addChildNode(cameraNode)
        scene.rootNode.addChildNode(lightNode)
        scene.rootNode.addChildNode(ambientLightNode)
        
//        let trans = SKTransition.doorway(withDuration: 1)
//        let trans = SKTransition.crossFade(withDuration: 1)
        let trans = SKTransition.fade(with: .red, duration: 1)
        sceneView?.present(scene, with: trans, incomingPointOfView: cameraNode, completionHandler: {
            print("")
        })
        
//        sceneView?.scene = scene
//        sceneView?.backgroundColor = UIColor.cyan
    }

}
